/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pedircita.java
 *
 * Created on 24/11/2010, 07:43:53 AM
 */

package GUI.clinica;

import java.text.SimpleDateFormat;
import java.util.Date;
import Persistencia.*;
import cprincipal.*;
import javax.swing.JOptionPane;

import java.util.Calendar;
import java.text.DateFormat;

/**
 *
 * @author Administrador
 */
public class pedirCita extends javax.swing.JFrame {

  //  String cedula;

    /** Creates new form pedircita */


    CitaXmlClinica datosCita=new CitaXmlClinica();
    HistorialMedicoXml datosHist=new HistorialMedicoXml();

    public pedirCita() {
        initComponents();
    }
//    public pedirCita(String hola){
 //    this.cedula= hola;
 //   }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        LabelNombreDoctor = new javax.swing.JLabel();
        TxtNombreDoctor = new javax.swing.JTextField();
        LabelCedulaPaciente = new javax.swing.JLabel();
        BotonApartarCita = new javax.swing.JButton();
        BotonRegresar = new javax.swing.JButton();
        TxtCedulaPaciente = new javax.swing.JTextField();
        LabelCedulaDoctor = new javax.swing.JLabel();
        TxtCedulaDoctor = new javax.swing.JTextField();
        LabelFechaCita = new javax.swing.JLabel();
        LabelHora = new javax.swing.JLabel();
        ComboBoxHora = new javax.swing.JComboBox();
        jDateChooser = new com.toedter.calendar.JDateChooser();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setForeground(new java.awt.Color(153, 0, 0));
        jLabel1.setText("Apartado de citas");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        LabelNombreDoctor.setText("Nombre Doctor:");

        LabelCedulaPaciente.setText("Cedula Paciente:");

        BotonApartarCita.setText("Apartar cita");
        BotonApartarCita.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonApartarCitaActionPerformed(evt);
            }
        });

        BotonRegresar.setText("Regresar");
        BotonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonRegresarActionPerformed(evt);
            }
        });

        LabelCedulaDoctor.setText("Cedula Doctor:");

        LabelFechaCita.setText("Fecha de Cita:");

        LabelHora.setText("Hora:");

        ComboBoxHora.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-", "7:00 am", "8:00 am", "9:00 am", "10:00 am", "11:00 am", "12:00 pm", "1:00 pm", "2:00 pm", "3:00 pm", "4:00 pm", "5:00 pm", "6:00 pm ", "7:00 pm", "8:00 pm", "9:00 pm ", "10:00 pm", "11:00 pm", "12:00 am", "1:00 am", "2:00 am", "3:00 am", "4:00 am", "5:00 am", "6:00 am" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addComponent(LabelCedulaPaciente)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(TxtCedulaPaciente))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addComponent(LabelHora)
                            .addGap(5, 5, 5)
                            .addComponent(ComboBoxHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(LabelNombreDoctor)
                                .addComponent(LabelCedulaDoctor)
                                .addComponent(LabelFechaCita))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(10, 10, 10)
                                    .addComponent(TxtCedulaDoctor))
                                .addComponent(jDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(TxtNombreDoctor))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(BotonApartarCita)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                        .addComponent(BotonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LabelCedulaPaciente)
                    .addComponent(TxtCedulaPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LabelNombreDoctor)
                    .addComponent(TxtNombreDoctor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LabelCedulaDoctor)
                    .addComponent(TxtCedulaDoctor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(LabelFechaCita)
                    .addComponent(jDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LabelHora)
                    .addComponent(ComboBoxHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BotonApartarCita)
                    .addComponent(BotonRegresar))
                .addContainerGap(72, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BotonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonRegresarActionPerformed
         clinica ventana =  new clinica();
           ventana.setVisible(true);
           this.dispose();
    }//GEN-LAST:event_BotonRegresarActionPerformed

    private void BotonApartarCitaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonApartarCitaActionPerformed
        // TODO add your handling code here:
        //String[] listaFecha = fechaString.split("/");

        //System.out.println(fechaString);

        /*int dia, mes, anio;

        dia = Integer.parseInt(listaFecha[0]);
        mes = Integer.parseInt(listaFecha[1]);
        anio = Integer.parseInt(listaFecha[2]);*/

  if (!(TxtCedulaPaciente.getText().isEmpty()) && !(TxtCedulaDoctor.getText().isEmpty())&& !(ComboBoxHora.getSelectedItem().equals("-"))){
        
    //         int valor1 = Validar.ValidarEntero(TxtCedulaDoctor.getText(), "Error, cedula del doctor no es numerica");

      //              if (valor1==1){

        //                    int valor2 = Validar.ValidarEntero(TxtCedulaPaciente.getText(), "Error, cedula del paciente no es numerica");

          //                       if (valor2==2){

                                    DoctorXml datosDoc = new DoctorXml();
                                    Doctor doc = datosDoc.buscarDoctor(Integer.parseInt(TxtCedulaDoctor.getText()));

                                    Boolean doctor =false;
                                    Boolean paciente= false;
                                    if (doc ==null){
                                        JOptionPane.showMessageDialog(null, "Doctor no registrado en sistema ", "Error", JOptionPane.ERROR_MESSAGE);
                                        TxtCedulaDoctor.setText("");
                                        TxtCedulaPaciente.setText("");
                                        }
                                    else doctor = true;


                                    PacienteXml datosPac = new PacienteXml();
                                    Paciente pac = datosPac.buscarPaciente(Integer.parseInt(TxtCedulaPaciente.getText()));
                                    if(pac == null){
                                         JOptionPane.showMessageDialog(null, "Paciente no registrado en sistema", "Error", JOptionPane.ERROR_MESSAGE);
                                         informacionPaciente ventana =  new informacionPaciente(pac);
                                         ventana.setVisible(true);
                                    }else paciente = true;



                                    Date fecha = new Date();
                                    
                                    fecha = jDateChooser.getDate();

                                    SimpleDateFormat formatoFecha = new SimpleDateFormat("dd/MM/yyyy");

                                    String fechaString = formatoFecha.format(fecha).toString();

                                    String horaString = ComboBoxHora.getSelectedItem().toString();

                                    /*String hora = "";

                                    String[] listaFecha = fechaString.split("/");

                                    int dia, mes, anio;

                                    dia = Integer.parseInt(listaFecha[0]);
                                    mes = Integer.parseInt(listaFecha[1]);
                                    anio = Integer.parseInt(listaFecha[2]);*/

                                    /*if (doc == null) {

                                        JOptionPane.showMessageDialog(null, "Doctor no registrado en sistema ", "Error", JOptionPane.ERROR_MESSAGE);
                                        TxtCedulaDoctor.setText("");
                                        TxtCedulaPaciente.setText("");

                                    }else{

                                        if (doc.getDisponible().equals("Si")){
                                                if (pac == null){

                                                     JOptionPane.showMessageDialog(null, "Paciente no registrado en sistema", "Error", JOptionPane.ERROR_MESSAGE);
                                                     facturacion ventana =  new facturacion();
                                                     ventana.setVisible(true);
                                                     this.dispose();

                                                }else{*/
                                    if(paciente && doctor){
                                                     //System.out.println("entre a agregar cita");
                                                     Citas cita = new Citas((doc.getNombre()+" "+doc.getApellido()),Integer.parseInt(TxtCedulaDoctor.getText()),(pac.getNombre()+" "+pac.getApellido()),Integer.parseInt(TxtCedulaPaciente.getText()),doc.getDisponible(), fechaString,horaString);
                                                     if (((datosCita.buscarCita2(Integer.parseInt(TxtCedulaDoctor.getText()), fechaString, horaString))==null)&&((cita.getDisponible().equals("Si")))){
                                                         //cita = datosCita.buscarCita(Integer.parseInt(TxtCedulaDoctor.getText()), fechaString, horaString);
                                                          // if ((cita.getDisponible().equals("Si"))&&()){
                                                                    datosCita.agregarCita(cita);
                                                                    JOptionPane.showMessageDialog(null, "Cita creada con exito");
                                                                    TxtNombreDoctor.setText("");
                                                                    TxtCedulaDoctor.setText("");
                                                                    TxtCedulaPaciente.setText("");
                                                                    ComboBoxHora.setSelectedItem("-");
                                                                    jDateChooser.setCalendar(null);
                                                         //  }
                                                    }else{
                                                        JOptionPane.showMessageDialog(null, "Fecha ya tiene previa cita, Elija nuevamente una fecha");
                                                    }
                                                     ////Citas cita = new Citas(TxtCedulaDoctor.getText(),TxtCedulaPaciente.getText(),fechaString,horaString);
                                                     ////datosCita.agregarCita(doc, pac, fechaString, horaString);
                                                        ////System.out.println(datosCita.buscar(cita, horaString));
                                      }
                                                           /* }
                                        }else{

                                                JOptionPane.showMessageDialog(null, "Error, Doctor fuera de servicio", "Error", JOptionPane.ERROR_MESSAGE);
                                                TxtCedulaDoctor.setText("");
                                                TxtCedulaPaciente.setText("");
                                        }

                                    }*/
                        /*       }
                  }*/
        }else{
             JOptionPane.showMessageDialog(null, "Debe llenar todos los campos ", "Error", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_BotonApartarCitaActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new pedirCita().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonApartarCita;
    private javax.swing.JButton BotonRegresar;
    private javax.swing.JComboBox ComboBoxHora;
    private javax.swing.JLabel LabelCedulaDoctor;
    private javax.swing.JLabel LabelCedulaPaciente;
    private javax.swing.JLabel LabelFechaCita;
    private javax.swing.JLabel LabelHora;
    private javax.swing.JLabel LabelNombreDoctor;
    private javax.swing.JTextField TxtCedulaDoctor;
    private javax.swing.JTextField TxtCedulaPaciente;
    private javax.swing.JTextField TxtNombreDoctor;
    private com.toedter.calendar.JDateChooser jDateChooser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

}
